<?php

  class YachtDice
  {
    public static $numDice = 5;
    public static $diceSides = 8;
    
    private static $categoryNames = array(
      'ones', 'twos', 'threes', 'fours', 'fives', 'sixes', 'sevens', 'eights',
      'threeOfKind', 'fourOfKind',
      'fullHouse',
      'smallStraight', 'largeStraight',
      'chance',
      'allDifferent', 'allSame'
    );
    
    private $dice;
    private $categories = array();
    
    public function __construct()
    {
      //initialize dice
      $this->dice = new Dice();
      
      //instantiate categories
      if (count(self::$categoryNames) > 0) {
        foreach (self::$categoryNames as $categoryName) {
          $ucCategoryName = ucfirst($categoryName);
          $categoryClassName = "{$ucCategoryName}Category";
          $this->categories[] = new $categoryClassName($ucCategoryName, $this->dice);
        }
      }
      
      //sort categories by score highest to lowest
      usort($this->categories, array($this, "categorySorter"));
    }
    
    public function __destruct()
    {
      //output dice values
      $diceValues = $this->dice->getValues();
      if (count($diceValues) > 0) {
        foreach ($diceValues as $key => $die) {
          echo "Die #{$key}: <strong>{$die}</strong><br />".PHP_EOL;
        }
      }

      //output result table
      echo "<table>".PHP_EOL;
      echo "  <thead>".PHP_EOL;
      echo "    <tr>".PHP_EOL;
      echo "      <td>Category</td>".PHP_EOL;
      echo "      <td>Score</td>".PHP_EOL;
      echo "    </tr>".PHP_EOL;
      echo "  </thead>".PHP_EOL;
      echo "  <tbody>".PHP_EOL;
      if (count($this->categories) > 0) {
        foreach ($this->categories as $category) {
          $category->output();
        }
      }
      echo "  </tbody>".PHP_EOL;
      echo "</table>";
    }
    
    private function categorySorter(Category $category1, Category $category2)
    {
      if ($category1->getScore() < $category2->getScore()) {
        return true;
      } else {
        return false;
      }
    }
  }

?>